﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZEFIT
{
    public partial class RapportsForm : Form
    {
        public MainForm _mainForm = null;
        public Controller controller = null;
        BindingList<Rapport> bindingList = null;

        public RapportsForm(MainForm tempForm)
        {
            InitializeComponent();
            this._mainForm = tempForm;
            controller = new Controller(new MYSQLAdabterRapport());
            dataLoad();
        }

        public void dataLoad()
        {
            bindingList = null;
            bindingList = new BindingList<Rapport>();
            foreach (List<object> tempList in controller.Select())
            {
                Rapport temp = new Rapport();
                temp.Id = (int)tempList[0];
                temp.WorkedFrom = (DateTime)tempList[1];
                temp.WorkedTo = (DateTime)tempList[2];
                User temp2 = new User();
                temp2.Id = (int)tempList[3];
                temp2.UserName = (String)tempList[4];
                temp.User = temp2;
                WorkingCategory temp3 = new WorkingCategory();
                temp3.Id = (int)tempList[5];
                temp3.Name = (String)tempList[6];
                temp.Category = temp3;
                Project temp4 = new Project();
                temp4.Id = (int)tempList[7];
                temp4.Name = (String)tempList[8];
                temp.Project = temp4;
                bindingList.Add(temp);
            }
            rapportBindingSource.DataSource = bindingList;

        }
        private void RapportsForm_FormClosed(object sender, FormClosedEventArgs e) 
        {
            this._mainForm.openForm();
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            Rapport temp = (Rapport)dataGridView1.SelectedRows[0].DataBoundItem;
            if (temp != null)
            {
                NewRapportForm newRapport = new NewRapportForm(this, temp);
                newRapport.ShowDialog();
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            Rapport temp = (Rapport)dataGridView1.SelectedRows[0].DataBoundItem;
            if (temp != null)
            {
                controller.Delete(temp);
                dataLoad();
            }
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            NewRapportForm newRapport = new NewRapportForm(this);
            newRapport.ShowDialog();
        }

        public void back()
        {
            dataLoad();
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            this._mainForm.openForm();
            this.Close();
        }
    }
}